<?php

namespace app\models;

use Yii;
use app\models\alilib\AlipaySubmit;
use app\models\Recharge;
use app\models\YeDetail;
/**
 * This is the model class for table "{{%sys_balance}}".
 *
 * @property integer $id
 * @property integer $uid
 * @property double $account_ye
 * @property double $account_bzj
 * @property double $account_djbzj
 * @property string $password
 * @property integer $created_at
 * @property integer $status
 */
class Balance extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%sys_balance}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['uid'], 'required'],
            [['uid', 'created_at', 'status'], 'integer'],
            [['account_ye', 'account_bzj', 'account_djbzj'], 'number'],
            [['password'], 'string', 'max' => 120]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'uid' => '用户ID',
            'account_ye' => '余额',
            'account_bzj' => '可用保证金',
            'account_djbzj' => '冻结保证金',
            'password' => '支付密码',
            'created_at' => '更新时间',
            'status' => '1正常 2冻结',
        ];
    }
    /**

     * author:sunjian
     * time:2016年2月17日14:31:17
     * params: orderId,supId，pricef
     * content: 用支付宝支付订单
     * return :bool
     *      */
    static function payUseAli($price,$orderId) {
        //引入阿里配置
       header("Content-type:text/html;charset=utf-8");
       require_once(__DIR__ . '/../config/alipay.config.php');

        //支付类型
        $payment_type = "1";
        //必填，不能修改
        //服务器异步通知页面路径
        $notify_url =ALIPAY_URL. "balance/alipay-notice";
        //需http://格式的完整路径，不能加?id=123这类自定义参数
        //页面跳转同步通知页面路径
        $return_url =ALIPAY_URL. "balance/alipay-return";
        //需http://格式的完整路径，不能加?id=123这类自定义参数，不能写成http://localhost/
        //商户订单号
        $out_trade_no = $orderId;
        //商户网站订单系统中唯一订单号，必填
        //订单名称
        $subject = '找化妆品网';
        //必填
        //付款金额
       // $total_fee = $price;
        $total_fee = 0.01;
        //必填
        //订单描述

        $body = "订单号：".$orderId;
        //商品展示地址
        $show_url = 'http://www.zhaohuazhaungpin.com';
        //需以http://开头的完整路径，例如：http://www.商户网址.com/myorder.html
        //防钓鱼时间戳
        $anti_phishing_key = "";
        //若要使用请调用类文件submit中的query_timestamp函数
        //客户端的IP地址
        $exter_invoke_ip = "";
        //非局域网的外网IP地址，如：221.0.0.1
        $parameter = array(
            "service" => "create_direct_pay_by_user",
            "partner" => trim($alipay_config['partner']),
            "seller_email" => trim($alipay_config['seller_email']),
            "payment_type" => $payment_type,
            "notify_url" => $notify_url,
            "return_url" => $return_url,
            "out_trade_no" => $out_trade_no,
            "subject" => $subject,
            "total_fee" => $total_fee,
            "body" => $body,
            "show_url" => $show_url,
            "anti_phishing_key" => $anti_phishing_key,
            "exter_invoke_ip" => $exter_invoke_ip,
            "_input_charset" => trim(strtolower($alipay_config['input_charset']))
        );
        $alipaySubmit = new AlipaySubmit($alipay_config);
        $html_text = $alipaySubmit->buildRequestForm($parameter, "get", "确认");
        echo $html_text;
        
    }
    static function alipayFinish($alipayNo,$orderId){
        $recharge= Recharge::findOne(['orderid'=>$orderId]);
        if($recharge){
            $recharge->status=1;
            $recharge->dksj=time();
            $recharge->audit_time=time();
            if($recharge->save()){
                $yedetail=new YeDetail();
                $yedetail->uid=$recharge->uid;
                $yedetail->type=1;
                $yedetail->account=$recharge->account;
                $yedetail->detail="支付宝成功支付：".$alipayNo;
                $yedetail->result="充值成功";
                $yedetail->create_time=time();
                $yedetail->operator='系统';
                $yedetail->orderid=$orderId;
                if($yedetail->save()){
                    $balance=  self::findOne(['uid'=>$recharge->uid]);
                    $balance->account_ye=$balance->account_ye+$recharge->account;
                    if($balance->save()){
                        
                    }else{
                        $recharge->attributes=$recharge->oldAttributes;
                        $recharge->save();
                        $yedetail->delete();
                    }
                }else{
                    $recharge->attributes=$recharge->oldAttributes;
                    $recharge->save();
                }
            }
        }
    }
}
